/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.io.*;
import java.util.stream.Collectors;

/**
 * <p>Project: app2022 - Exam02
 * <p>Powered by webrx On 2022-01-19 16:35:02
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Exam02 {
    public static void main(String[] args) {
        //项目目录
        String dir = System.getProperty("user.dir");
        var e = new Exam02();
        e.count(new File(dir));
    }

    public void append(String source, File dst) {
        try (var out = new BufferedWriter(new FileWriter(dst, true))) {
            out.write(source);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void append(File src, File dst) {
        try (var read = new BufferedReader(new FileReader(src))) {
            StringBuilder sbu = new StringBuilder();
            sbu.append("-----------------------------------------------------------------------------------------\n");
            sbu.append(String.format("--文件名称：%s%n", src.getName()));
            sbu.append(String.format("--文件路径：%s%n", src.getAbsolutePath()));
            sbu.append("-----------------------------------------------------------------------------------------\n");
            String info = read.lines().collect(Collectors.joining("\n"));
            sbu.append(info + "\n\n");
            append(sbu.toString(), dst);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void count(File dir) {
        for (File t : dir.listFiles()) {
            if (t.isDirectory()) {
                count(t);
            } else if (t.getName().endsWith(".java")) {
                append(t, new File("src.txt"));
                //System.out.println(t.getAbsolutePath());
            }
        }
        if (dir.isFile() && dir.getName().endsWith(".java")) {
            System.out.println(dir);
        }
    }
}
